package com.yohann.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.commonutils.result.R;
import com.yohann.eduservice.entity.EduCourse;
import com.yohann.eduservice.entity.vo.CourseInfo;
import com.yohann.eduservice.entity.vo.CoursePublishVo;
import com.yohann.eduservice.entity.vo.CourseQuery;
import com.yohann.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/edu-service/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 添加课程
     * @param courseInfo
     * @return
     */
    @PostMapping("add")
    public R addCourseInfo(@RequestBody CourseInfo courseInfo){
        String id = courseService.saveCourseInfo(courseInfo);
        return R.ok().data("id",id);
    }

    /**
     * 通过id获取课程信息
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfo courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    /**
     * 修改课程信息
     * @param courseInfo
     * @return
     */
    @PostMapping("update")
    public R updateCourseInfo(@RequestBody CourseInfo courseInfo){
        courseService.updateCourseInfo(courseInfo);

        return R.ok();
    }

    /**
     * 确认发布信息
     * @param id
     * @return
     */
    @GetMapping("getCoursePublishVoById/{id}")
    public R getCoursePublishVoById(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);

        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    /**
     * 发布课程
     * @param id
     * @return
     */
    @GetMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");

        courseService.updateById(course);

        return R.ok();
    }

    /**
     * 查询课程
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @GetMapping("list/{current}/{limit}")
    public R pageQueryTeacher(@PathVariable long current,
                              @PathVariable long limit,
                              CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(current, limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (courseQuery != null){
            if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
                wrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
            }
            if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
                wrapper.eq("subject_id", courseQuery.getSubjectId());
            }
            if (!StringUtils.isEmpty(courseQuery.getTeacherId())) {
                wrapper.eq("teacher_id", courseQuery.getTeacherId());
            }
            if (!StringUtils.isEmpty(courseQuery.getTitle())) {
                wrapper.like("title", courseQuery.getTitle());
            }
        }

        wrapper.orderByDesc("gmt_create");

        IPage<EduCourse> coursePage = courseService.page(page, wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", coursePage.getTotal());
        map.put("rows", coursePage.getRecords());

        return R.ok().data(map);
    }

    /**
     * 通过id删除课程
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R deleteCourse(@PathVariable String id){
        boolean flag = courseService.DeleteCourse(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

